package com.keat.qqserver.service;

import com.keat.qqcommon.Message;
import com.keat.qqcommon.MessageType;
import com.keat.qqcommon.User;
import com.sun.org.apache.bcel.internal.generic.NEW;

import java.awt.*;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author Keat-Jie
 * @version 1.0
 * 服务端，监听9999
 */
public class QQServer {
    private ServerSocket ss = null;
    //可以使用    ConcurrentHashMap处理并发
    private static ConcurrentHashMap<String,User> validUsers = new ConcurrentHashMap<>();

    static {
        validUsers.put("bobo",new User("bobo","230928"));
        validUsers.put("ji",new User("ji","jijiji"));
        validUsers.put("jiojio",new User("jiojio","21518"));
        validUsers.put("ss",new User("ss","zy000000"));
        validUsers.put("feiyangyang",new User("feiyangyang","tgbdhs"));
        validUsers.put("x0",new User("","123456"));
        validUsers.put("yub",new User("yub","222"));
    }
    private boolean checkUser(String userId,String passwd){
        User user = validUsers.get(userId);
        if (user == null){//user不存在
             return false;
        }
        if (!user.getPasswd().equals(passwd)){//密码错误
            return false;
        }
        return true;
    }


    public QQServer() {
        try {
            System.out.println("服务端在9999端口监听");
            new Thread(new SendNewsToAllService()).start();
            ss = new ServerSocket(9999);
            while (true) {
                Socket socket = ss.accept();
                ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
                ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
                User u = (User) ois.readObject();
                Message message = new Message();
                if (checkUser(u.getUserId(), u.getPasswd())) {
                    //登陆成功
                    message.setMesType(MessageType.MESSAGE_LOGIN_SUCCEED);
                    oos.writeObject(message);
                    ServerConnectClientThread serverConnectClientThread =
                            new ServerConnectClientThread(socket, u.getUserId());
                    serverConnectClientThread.start();
                    ManageClientThreads.addClientThread(u.getUserId(), serverConnectClientThread);
                    ConcurrentHashMap<String,ArrayList> offlineMap = OffLineMessageService.getOfflineMap();
                    OffLineMessageService.sendOfflineMessage(u.getUserId(),offlineMap);
                    OffLineMessageService.deleteOfflineMessage(u.getUserId());
                } else {
                    System.out.println("用户 id= " + u.getUserId() + " pwd= " +  u.getPasswd() + "登录失败");
                    message.setMesType(MessageType.MESSAGE_LOGIN_FAIL);
                    oos.writeObject(message);
                    socket.close();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //服务端退出了while循环,关闭资源
            try {
                ss.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
